package com.anran0.calculator;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    //—————————————逻辑—————————————
    //用户上一个输入的数据
    Double previousNnumber = null;
    //用户正在输入中的数据
    StringBuffer editingNuber = new StringBuffer();
    //当前的运算符
    Operator operatorNow = null;

    //用户按钮与运算的映射
    Map<Button, Operator> operatorMap = new HashMap<>();

    //———————————————————View————————————————————————————

    /**
     * 界面上运算结果的显示
     */
    TextView tvResult;
    //界面上数字的按钮
    Button btNumbers[] = new Button[10];
    Button btNumberPoint;
    //界面上的运算符按钮
    Button btAddition, btSubtraction, btMultiplication, btDivision;
    //界面上的等于号按钮
    Button btEqualto;
    //界面上的相反符号
    Button btMirror;
    //界面上的清除符号
    Button btClean;
    //界面上的百分比符号
    Button btPercentage;
    /**
     * 用户当前正在输入的公式的预览
     */
    TextView tvFormula;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initTextView();

        initInputNumberButton();
        initOperatorButton();

        initSpecialButton();


    }

    //——————————初始化按钮——————————————

    /**
     * 初始化界面上的特殊按钮
     */
    private void initSpecialButton() {
        //初始化百分比按钮
        btPercentage = findViewById(R.id.bt_percentage);
        btPercentage.setOnClickListener(new CalculatiorButtonClickListener() {
            @Override
            public void onClick(View v) {


                double d = Double.valueOf(editingNuber.toString()) * 0.01;
                editingNuber.setLength(0);
                editingNuber.append(d);

                super.onClick(v);

            }
        });


        //初始化清空按钮
        btClean = findViewById(R.id.bt_clean);
        btClean.setOnClickListener(new CalculatiorButtonClickListener() {
            @Override
            public void onClick(View v) {
                //用户上一个输入的数据
                previousNnumber = null;
                //用户正在输入中的数据
                editingNuber = new StringBuffer();
                //当前的运算符
                operatorNow = null;

                tvResult.setText("");
                tvFormula.setText("");


                super.onClick(v);
            }
        });

        //初始化相反
        btMirror = findViewById(R.id.bt_mirror);
        btMirror.setOnClickListener(new CalculatiorButtonClickListener() {
            @Override
            public void onClick(View v) {


                double d = Double.valueOf(editingNuber.toString()) * -1;
                editingNuber.setLength(0);
                editingNuber.append(d);

                super.onClick(v);
            }
        });

        //初始化等于号
        btEqualto = findViewById(R.id.bt_equalto);
        btEqualto.setOnClickListener(new CalculatiorButtonClickListener() {
            @Override
            public void onClick(View v) {
                super.onClick(v);
                Double result = operatorNow.operation(previousNnumber, Double.valueOf(editingNuber.toString()));
                tvResult.setText(result.toString());
                super.onClick(v);
            }
        });
    }

    /**
     * 初始化界面上的TextView
     */
    private void initTextView() {
        tvResult = findViewById(R.id.tv_result);
        tvFormula = findViewById(R.id.tv_formula);
        tvResult.setText("");
        tvFormula.setText("");
    }


    /**
     * 初始化用户界面上的运算符
     */
    private void initOperatorButton() {

        btAddition = findViewById(R.id.bt_addition);
        btSubtraction = findViewById(R.id.bt_subtraction);
        btMultiplication = findViewById(R.id.bt_multiplication);
        btDivision = findViewById(R.id.bt_division);

        operatorMap.put(btAddition, addition);
        operatorMap.put(btSubtraction, subtraction);
        operatorMap.put(btMultiplication, multiplication);
        operatorMap.put(btDivision, division);


        View.OnClickListener listener = new CalculatiorButtonClickListener() {
            @Override
            public void onClick(View v) {
                //如果用户进行了连续运算
                //待完成
                if (previousNnumber != null && operatorNow != null && editingNuber.length() > 0) {
                    Double result = operatorNow.operation(previousNnumber, Double.valueOf(editingNuber.toString()));
                    tvResult.setText(result.toString());
                    previousNnumber = result;
                }
                //如果用户只是在进行第一次运算
                //这里需要将用户当前正在输入的数据，转换为用户已经输入的数据
                //获取用户当前的运算符
                else {
                    previousNnumber = Double.valueOf("0" + editingNuber.toString());
                }
                editingNuber.setLength(0);
                operatorNow = operatorMap.get(v);

                super.onClick(v);

            }
        };
        btAddition.setOnClickListener(listener);
        btSubtraction.setOnClickListener(listener);
        btMultiplication.setOnClickListener(listener);
        btDivision.setOnClickListener(listener);
    }

    /**
     * 初始化用户界面上的数字按钮
     */
    private void initInputNumberButton() {
        {
            int k = 0;
            btNumbers[k++] = findViewById(R.id.bt_number0);
            btNumbers[k++] = findViewById(R.id.bt_number1);
            btNumbers[k++] = findViewById(R.id.bt_number2);
            btNumbers[k++] = findViewById(R.id.bt_number3);
            btNumbers[k++] = findViewById(R.id.bt_number4);
            btNumbers[k++] = findViewById(R.id.bt_number5);
            btNumbers[k++] = findViewById(R.id.bt_number6);
            btNumbers[k++] = findViewById(R.id.bt_number7);
            btNumbers[k++] = findViewById(R.id.bt_number8);
            btNumbers[k++] = findViewById(R.id.bt_number9);

        }
        btNumberPoint = findViewById(R.id.bt_numberpoint);

        //将用户点击的数字按钮添加进用户正在输入的数据
        for (int i = 0; i < btNumbers.length; i++) {
            final int k = i;
            btNumbers[i].setOnClickListener(new CalculatiorButtonClickListener() {
                @Override
                public void onClick(View v) {
                    editingNuber.append(k);
                    super.onClick(v);
                }
            });
        }

        //将用户点击的小数点添加进用户正在输入的数据

        btNumberPoint.setOnClickListener(new CalculatiorButtonClickListener() {
            @Override
            public void onClick(View v) {
                editingNuber.append('.');

                super.onClick(v);
            }
        });
    }



    //—————————————————逻辑为逻辑解决需求问题—————————————————————

    /**
     * 界面上按钮点击事件的公用父类，用来刷新界面上的显示
     */
    public class CalculatiorButtonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            StringBuffer buffer = new StringBuffer();
            if (previousNnumber != null)
                buffer.append(previousNnumber);
            if (operatorNow != null)
                buffer.append(operatorNow.toString());
            buffer.append(editingNuber);
            tvFormula.setText(buffer);
        }
    }


    /**
     * 加法运算
     */
    Operator addition = new Operator() {
        @Override
        public double operation(double a, double b) {
            return a + b;
        }

        @NonNull
        @Override
        public String toString() {
            return "+";
        }
    };
    /**
     * 减法运算
     */
    Operator subtraction = new Operator() {
        @Override
        public double operation(double a, double b) {
            return a - b;
        }

        @NonNull
        @Override
        public String toString() {
            return "-";
        }
    };
    /**
     * 乘法运算
     */
    Operator multiplication = new Operator() {
        @Override
        public double operation(double a, double b) {
            return a * b;
        }

        @NonNull
        @Override
        public String toString() {
            return "×";
        }
    };
    /**
     * 除法运算
     */
    Operator division = new Operator() {
        @Override
        public double operation(double a, double b) {
            return a / b;
        }

        @NonNull
        @Override
        public String toString() {
            return "÷";
        }
    };

    /**
     * 运算符接口
     */
    public static interface Operator {
        double operation(double a, double b);
    }
}


